{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "senior-herald",
   "metadata": {},
   "source": [
    "# Example 14.2 Joule-Thomson Effect"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "narrative-examination",
   "metadata": {},
   "source": [
    "A stream of nitrogen is expanded from T1 = 300 K, P1 = 200 bar, to 1 bar by a throttling valve. An ideal throttling valve has the conditions of being adiabatic (no heat loss, energy is conserved); and is either solved using a valve Cv to solve for pressure or solved with the outlet pressure directly specified. \n",
    "\n",
    "Calculate the outlet temperature using:\n",
    "\n",
    "(1) A high precision (helmholtz fundamental) equation of state\n",
    "\n",
    "(2) The Peng-Robinson equation of state"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "wireless-aaron",
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set the conditions and imports\n",
    "from scipy.constants import bar\n",
    "from thermo import ChemicalConstantsPackage, PRMIX, CEOSLiquid, CoolPropLiquid, CEOSGas, CoolPropGas, FlashPureVLS\n",
    "fluid = 'nitrogen'\n",
    "constants, correlations = ChemicalConstantsPackage.from_IDs([fluid])\n",
    "\n",
    "T1 = 300.0\n",
    "P1 = 200*bar\n",
    "P2 = 1*bar\n",
    "zs = [1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "lightweight-migration",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "269.1866854380218"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Thermo can use CoolProp to provide properties of one or all phases\n",
    "# For pure species this is quite reliable within the temperature,\n",
    "# pressure, etc. limits of the EOSs implemented by CoolProp \n",
    "\n",
    "backend = 'HEOS'\n",
    "gas = CoolPropGas(backend, fluid, T=T1, P=P1, zs=zs)\n",
    "liquid = CoolPropLiquid(backend, fluid, T=T1, P=P1, zs=zs)\n",
    "\n",
    "flasher = FlashPureVLS(constants, correlations, gas=gas, liquids=[liquid], solids=[])\n",
    "\n",
    "state_1 = flasher.flash(T=T1, P=P1)\n",
    "state_2 = flasher.flash(H=state_1.H(), P=P2)\n",
    "T2_precise = state_2.T\n",
    "T2_precise"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "trained-burke",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "265.400194181466"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# Use the default originally published Peng-Robinson models\n",
    "eos_kwargs = dict(Tcs=constants.Tcs, Pcs=constants.Pcs, omegas=constants.omegas)\n",
    "liquid = CEOSLiquid(PRMIX, HeatCapacityGases=correlations.HeatCapacityGases, eos_kwargs=eos_kwargs)\n",
    "gas = CEOSGas(PRMIX, HeatCapacityGases=correlations.HeatCapacityGases, eos_kwargs=eos_kwargs)\n",
    "flasher = FlashPureVLS(constants, correlations, gas=gas, liquids=[liquid], solids=[])\n",
    "\n",
    "state_1 = flasher.flash(T=T1, P=P1)\n",
    "state_2 = flasher.flash(H=state_1.H(), P=P2)\n",
    "T2_PR = state_2.T\n",
    "T2_PR"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "voluntary-elite",
   "metadata": {},
   "source": [
    "The outlet temperature anwsers given in the book are 269.19 K for the high-precision EOS, and for the PR EOS they used a very low precision Cp of 1 J/(g*K) and obtained an outlet temperature of 283.05 K.\n",
    "\n",
    "The book textbook cites this 14 K difference as coming from the cubic EOS's lack of precision but the above calculation shows that if an accurate heat capacity is used the difference is only ~ 4K."
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
